<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01栈的封装</title>
  </head>
  <script>
    /*
    // Methods:与某一个对象有联系为方法
    // function 
    // 封装栈类
    function Stack() {
        // 栈的属性
        this.items = []
        // 栈的相关操作
        // 1.将元素压入栈
        // 共享方法给整个类添加方法
        Stack.prototype.push = function (element) {
            this.items.push(element)
        }
        // 给每一个实例添加方法
        // this.push = function () {

        // }
        // 2.从栈中取出元素
        Stack.prototype.pop = function () {
            return this.items.pop()
        }
        // 3.查看栈顶元素
        Stack.prototype.peek = function () {
            return this.items[this.items.length - 1]
        }
        // 4.判断栈中元素是否为空
        Stack.prototype.isEmpty = function () {
            return this.items.length == 0
        }
        // 5.获取元素个数
        Stack.prototype.size = function () {
            return this.items.length
        }
        // 6.toString方法
        Stack.prototype.toString = function () {
            var resultString = ''
            for (let i = 0; i < this.items.length; i++) {
                resultString = this.items[i] + ''
            }
            return resultString
        }

        Stack.prototype.clear = function () {
            return this.items = []
        }
    }
    //  栈的使用
    var s = new Stack()
    s.push(100)
    s.push(4)
    s.push(10)
    s.push(6)
    s.push(77)
    s.push(17)
    alert(s.peek())
    s.pop()
    alert(s.peek())
    s.pop()
    alert(s.peek())
    alert(s.items)
    alert(s.size())

*/
    // 栈类
    function Stack() {
      // 栈中的属性
      var items = []

      // 栈相关的方法
      // 压栈操作
      this.push = function(element) {
        items.push(element)
      }

      // 出栈操作
      this.pop = function() {
        return items.pop()
      }

      // peek操作
      this.peek = function() {
        return items[items.length - 1]
      }

      // 判断栈中的元素是否为空
      this.isEmpty = function() {
        return items.length == 0
      }

      // 获取栈中元素的个数
      this.size = function() {
        return items.length
      }
      this.toString = function() {
        resultString = items.join() + ''
        return resultString
      }
      this.clear = function() {
        return items = []
      }
    }
    // 模拟面试题
    var stack = new Stack()

    // 情况下代码模拟
    stack.push(6)
    stack.push(5)
    console.log(stack.toString())
    stack.pop() // 5
    stack.push(4)
    stack.pop() // 4
    stack.push(3)
    stack.pop() // 3
    stack.pop() // 6
    stack.push(2)
    stack.push(1)
    stack.pop() // 1
    stack.pop() // 2


    // 封装十进制转二进制的函数
    function dec2bin(decNumber) {
      // 定义变量
      var stack = new Stack()
      var remainder;

      // 循环除法
      while (decNumber > 0) {
        remainder = decNumber % 2
        decNumber = Math.floor(decNumber / 2)
        stack.push(remainder)
      }

      // 将数据取出
      var binaryString = ''
      while (!stack.isEmpty()) {
        binaryString += stack.pop()
      }
      return binaryString
    }

    // 测试函数
    alert(dec2bin(10))
    alert(dec2bin(233))
    alert(dec2bin(1000))
  </script>

  <body>
  </body>

</html>
